<?php

/**
 * 删除专辑封面模板AJAX
 * 
 * @copyright xinwei
 * @since 2012-03-26
 */

include_once($_SERVER["DOCUMENT_ROOT"] . "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/core/admin.inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/foodv2/inc.php");

$g_cgival = array();
$g_pro = array();
$g_show = array();


function check_cgi_pro()
{
	global $g_cgival, $g_pro, $g_show;
	
	if(! security_check::check_refer())
	{
		throw new Exception("来路不明");
	}
	
	//模板ID
	cgi::get($g_cgival["template_id"], "template_id", 0);
	$g_cgival["template_id"] = intval($g_cgival["template_id"]);
	if($g_cgival["template_id"] == 0)
	{
		throw new Exception("ID传入错误");
	}

}

function get_data()
{
	global $g_cgival, $g_pro, $g_show;
	
	$db = new db_food_album_cover_template();
	$user_db = new db_food_user();
	$admin_log = new admin_log();
	
	// 拿到当前站点的site_id
	$site_id = admin::admin_get_user_site_id();
	$admin_id = admin::admin_get_login_uid();	// 拿到当前管理员ID
	$admin_name = admin::admin_get_login_user_name();	// 拿到当前管理员用户名
	
	//检查模板是否存在
	$check = $db->admin_get_food_album_cover_template_by_ids($g_cgival["template_id"]);
	$num_rec = count(explode(",",$g_cgival["template_id"]));//接收到的专辑数量
	$num_check = count($check);//数据库中检索到的专辑数量
	
	if($num_rec != $num_check)
	{
		throw new Exception('该模板不存在');
	} 
	
	//检查权限是否正常 排除总站帐号
	$check = $db->admin_check_food_album_cover_template_site_by_ids_site($g_cgival["template_id"],$site_id);
	if($check)
	{
		throw new Exception("您无权操作该模板");
	}

	// 更新专辑审核状态
	$album_class = $db->admin_delete_food_album_cover_template_by_id($g_cgival["template_id"]);
	if(!$album_class)
	{
		throw new Exception("删除失败");
	}
	
	$admin_log->admin_insert_log_info($admin_id, $site_id, 13, "删除 专辑模板template_id={$g_cgival["template_id"]}");
}

$g_show["error"] = 0;
$g_show["errmsg"] = '';
try
{
	admin_check_login();
// 	admin_check_user_priv("foodv2/album_cover_template_add_post.php");
	
	check_cgi_pro();
	get_data();
}
catch(Exception $e)
{
	$g_show["error"] = 1;
	$g_show["errmsg"] = $e->getMessage();
}

show_pro();
exit;


function show_pro()
{
	global $g_cgival, $g_pro, $g_show;
	if(isset($_GET['debug']))
		print_r($g_show);
	echo output::json('json',$g_show);
}

?>